package com.example.test.mapper;

import com.example.test.bean.BaseOrders;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.test.bean.DTO.output.BaseOrdersListOutputDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 基础订单表 Mapper 接口
 * </p>
 *
 * @author dz
 * @since 2022-04-10
 */
@Mapper
public interface BaseOrdersMapper extends BaseMapper<BaseOrders> {
    //根据keyword查询
    @Select("SELECT\n" +
            "\tb.order_id,\n" +
            "\tb.ship_address,\n" +
            "\tb.serial_number,\n" +
            "\tc.customer_name,\n" +
            "\tb.order_state,\n" +
            "\tu.user_nickname,\n" +
            "\tb.sellMoney,\n" +
            "\tb.remarks,\n" +
            "\tb.create_time\n" +
            "FROM\n" +
            "\tbase_orders b\n" +
            "LEFT JOIN\n" +
            "\tcustomers c\n" +
            "ON\n" +
            "\tb.costomer_id = c.customer_id\n" +
            "LEFT JOIN\n" +
            "\tusers u\n" +
            "ON\n" +
            "\tb.order_approval_id = u.user_id\n" +
            "WHERE\n" +
            "\tb.ship_address like concat('%',#{keyword},'%')\n" +
            "AND\n" +
            "\tb.deleted = 0\n" +
            "LIMIT #{size} OFFSET #{offset}\n")
    List<BaseOrdersListOutputDTO> getOrderByKeyword(String keyword, Long size, Long offset);

    //根据id查询表单
    @Select("SELECT\n" +
            "\tb.order_id,\n" +
            "\tb.ship_address,\n" +
            "\tb.serial_number,\n" +
            "\tc.customer_name,\n" +
            "\tb.order_state,\n" +
            "\tu.user_nickname,\n" +
            "\tb.sellMoney,\n" +
            "\tb.remarks,\n" +
            "\tb.create_time\n" +
            "FROM\n" +
            "\tbase_orders b\n" +
            "LEFT JOIN\n" +
            "\tcustomers c\n" +
            "ON\n" +
            "\tb.costomer_id = c.customer_id\n" +
            "LEFT JOIN\n" +
            "\tusers u\n" +
            "ON\n" +
            "\tb.order_approval_id = u.user_id\n" +
            "WHERE\n" +
            "\tb.order_id = #{orderId}\n")
    BaseOrdersListOutputDTO getOrder(Long orderId);

    //根据id查询表单-基础部分
    @Select("SELECT\n" +
            "\tb.order_id,\n" +
            "\tb.ship_address,\n" +
            "\tb.serial_number,\n" +
            "\tc.customer_name,\n" +
            "\tb.order_state,\n" +
            "\tu.user_nickname,\n" +
            "\tb.sellMoney,\n" +
            "\tb.remarks,\n" +
            "\tb.create_time\n" +
            "FROM\n" +
            "\tbase_orders b\n" +
            "LEFT JOIN\n" +
            "\tcustomers c\n" +
            "ON\n" +
            "\tb.costomer_id = c.customer_id\n" +
            "LEFT JOIN\n" +
            "\tusers u\n" +
            "ON\n" +
            "\tb.order_approval_id = u.user_id\n" +
            "WHERE\n" +
            "\tb.order_id = #{orderId}\n")
    BaseOrdersListOutputDTO getSelectOrder(Long orderId);

    //根据serialNumber查询id
    @Select("SELECT\n" +
            "\torder_id\n" +
            "FROM\n" +
            "\tbase_orders\n" +
            "WHERE\n" +
            "\tserial_number = #{serialNumber}\n")
    Long getOrderIdBySerial(String serialNumber);

    //根据id查询基础表单
    @Select("SELECT\n" +
            "\torder_id,\n" +
            "\tserial_number,\n" +
            "\tship_address,\n" +
            "\tcostomer_id,\n" +
            "\torder_state,\n" +
            "\torder_approval_id,\n" +
            "\tsellMoney,\n" +
            "\tdeleted\n" +
            "FROM\n" +
            "\tbase_orders\n" +
            "WHERE\n" +
            "\torder_id = #{orderId}\n")
    BaseOrders getBaseById(Long orderId);
}
